home *** CD-ROM | disk | FTP | other *** search
Text File | 1988-01-08 | 10.4 KB | 384 lines | [TEXT/PJMM] |
- { Quadratic Equation Example program }
- { By Dave Kelly & Dave Smith }
- { ©MacTutor, 1988 }
-
- PROGRAM QuadraticPlotter;
-
- USES
- ROM85, PrintTraps, PlotGlobals, Misc, myPlotStuff;
-
- PROCEDURE crash;
- BEGIN
- ExitToShell;
- END;
-
- PROCEDURE InitMac;
- VAR
- err : OSErr;
- BEGIN
- MaxApplZone;
- MoreMasters;
- MoreMasters;
- MoreMasters;
- MoreMasters;
- MoreMasters;
- InitGraf(@thePort);
- InitFonts;
- InitWindows;
- InitMenus;
- TEInit;
- InitDialogs(@crash);
- InitCursor;
- FlushEvents(everyEvent, 0);
- Finished := false;
- dialogflg := false;
-
- color[1] := 33; {black}
- color[2] := 30; {white}
- color[3] := 205; {red}
- color[4] := 341; {green}
- color[5] := 409; {blue}
- color[6] := 273; {cyan}
- color[7] := 137; {magenta}
- color[8] := 69; {yellow}
-
- theWorld.machineType := 4; {Mac II}
- {err := SysEnvirons(1, theWorld); SysEnvirons not in LSP 1.11}
- typeOfMac := theWorld.machineType;
-
- IF (typeOfMac >= 0) AND (NGetTrapAddress(WNETrapNum, ToolTrap) = NGetTrapAddress(UnImplTrapNum, ToolTrap)) THEN
- BEGIN
- WNE := FALSE;
- doMessage('WaitNextEvent not implemented', '', '', ''); {Only true under Multifinder}
- END
- ELSE
- WNE := TRUE;
- mouseRgn := NewRgn;
- END;
-
- PROCEDURE initRects;
- VAR
- MemoryPtr : ^Integer;
- BEGIN
- MemoryPtr := pointer(mBarHeightGlobal); {System Global mBarHeight}
- mBarHeight := MemoryPtr^;
- screen := ScreenBits.Bounds; {current screen device}
- SetRect(DragArea, Screen.left + 4, Screen.top + mBarHeight + 4, Screen.right - 4, Screen.bottom - 4);
- SetRect(GrowArea, Screen.left + MinWidth, Screen.top + MinHeight, Screen.right - 8, Screen.bottom - 8);
- SetRect(PlotWindowRect, Screen.left + 15, Screen.top + 45, Screen.right - 75, Screen.bottom - 50);
- SetRect(ZoomRect, Screen.left + 4, Screen.top + mBarHeight + 4, Screen.right - 4, Screen.bottom - 4);
- END;
-
- PROCEDURE InitMyWindow;
- VAR
- windtype : integer;
- Visible : boolean;
- GoAway : boolean;
- RefVal : LongInt;
- title : str255;
- myPrint : THPrint;
- BEGIN
- Visible := false;
- windtype := documentProc + ZoomBox;
- GoAway := true;
- RefVal := 0;
- title := 'Quadratic Plotter';
- PlotWindow := NewWindow(NIL, PlotWindowRect, title, Visible, windtype, pointer(-1), GoAway, RefVal);
- PlotWindowPeek := WindowPeek(PlotWindow);
- SetPort(PlotWindow);
- TextFont(Geneva);
- TextSize(10);
- TextFace([]); {plain}
- TextMode(1); {Or}
- PenNormal;
- ForeColor(blackColor);
- BackColor(whiteColor);
- PlotDocHandle := DocHandle(NewHandle(sizeof(Document)));
- PlotWindowPeek^.refCon := LongInt(PlotDocHandle);
- PlotWindowPeek^.windowKind := userKind;
- PlotDocHandle^^.drawing := NIL;
- WITH PlotWindow^.portRect DO
- BEGIN
- SetRect(VCRect, right - (SBarWidth - 1), top - 1, right + 1, bottom - (SBarWidth - 2));
- SetRect(HCRect, left - 1, bottom - (SBarWidth - 1), right - (SBarWidth - 2), bottom + 1);
- SetRect(GrowRect, HCRect.right, HCRect.top, VCRect.right, HCRect.bottom);
- SetRect(PicRect, left, top, right - (SBarWidth - 1), bottom - (SBarWidth - 1));
- SetRect(PageRect, left, top, right - (SBarWidth - 1), bottom - (SBarWidth - 1));
- END; {of with }
- ClipRect(PlotWindow^.portRect);
- myPrint := THPrint(NewHandle(SIZEOF(TPrint)));
- PlotDocHandle^^.Print := myPrint;
- END;
-
- PROCEDURE InitMyMenus;
- VAR
- i : integer;
- BEGIN
- myMenus[AppleM] := GetMenu(AppleMenu);
- AddResMenu(myMenus[AppleM], 'DRVR');
- myMenus[FileM] := GetMenu(FileMenu);
- myMenus[EditM] := GetMenu(EditMenu);
- myMenus[ColorM] := GetMenu(ColorMenu);
- myMenus[OptionM] := GetMenu(OptionMenu);
- myMenus[GraphM] := GetMenu(GraphMenu);
- myMenus[AxisM] := GetMenu(AxisMenu);
- myMenus[BackgroundM] := GetMenu(BackgroundMenu);
-
- FOR i := 1 TO MenuCount DO
- InsertMenu(myMenus[i], 0);
- FOR i := SubMenuStart TO TotalMenuCount DO
- InsertMenu(myMenus[i], -1);
-
- GraphColor := 3;
- CheckItem(myMenus[GraphM], GraphColor, true);
- AxisColor := 1;
- CheckItem(myMenus[AxisM], AxisColor, true);
- BackgroundColor := 2;
- CheckItem(myMenus[BackgroundM], BackgroundColor, true);
-
- CheckItem(myMenus[OptionM], 1, true);
- Option := 1; {window rect printing}
-
- DrawMenuBar;
- END; {of proc}
-
- PROCEDURE doMouse (myEvent : EventRecord);
- VAR
- whereIsIt : integer;
- whichWindow : WindowPtr;
- localPt, globalPt : Point;
- oldPort : GrafPtr;
- BEGIN
- globalPt := myEvent.where;
- localPt := globalPt; {global coord of mouse}
- GlobalToLocal(localPt); {local coord of mouse}
- whereIsIt := FindWindow(globalPt, whichWindow);
- CASE whereIsIt OF
- inDesk : {0}
- doMessage('Mouse Click on Desktop.', '(Not handled in this program.)', '', '');
- inMenuBar : {1}
- doMenuBar(MenuSelect(globalPt));
- inSysWindow : {2}
- SystemClick(myEvent, whichWindow);
- inContent : {3}
- doContent(myEvent, whichWindow);
- inDrag : {4}
- doDrag(whichWindow, globalPt);
- inGrow : {5}
- doGrow(whichWindow, globalPt, False);
- inGoAway : {6}
- IF TrackGoAway(whichWindow, globalPt) THEN
- HideWindow(whichWindow);
- inZoomIn, InZoomOut : {7, 8}
- BEGIN
- IF TrackBox(whichWindow, globalPt, whereIsIt) THEN
- BEGIN
- GetPort(OldPort);
- SetPort(whichWindow); {safety device}
- EraseRect(whichWindow^.portRect);
- ZoomWindow(whichWindow, whereIsIt, True);
- doGrow(whichWindow, globalPt, True);
- SetPort(OldPort);
- END;
- END;
- OTHERWISE
- BEGIN
- END;
- END; {of whereIsIt}
- END;
-
- PROCEDURE doKeyDowns (myEvent : EventRecord);
- VAR
- ch : char;
- charCode : longInt;
- keyCode : longInt;
- BEGIN
- charCode := BitAnd(myEvent.Message, charCodeMask); {strip off key code}
- keyCode := BitShift(BitAnd(myEvent.Message, keyCodeMask), -8); {strip off char}
- ch := Chr(charCode); {get keyboard char}
- IF BitAnd(myEvent.Modifiers, CmdKey) = CmdKey THEN
- doMenuBar(MenuKey(ch)) { do menu command key}
- ELSE
- BEGIN { do keystroke }
- ParamText('No typing supported…', '', '', '');
- itemhit := CautionAlert(AlertDialog, NIL);
- END; { of do key stroke }
- END; { of proc}
-
- PROCEDURE doUpdates (myEvent : EventRecord);
- VAR
- UpdateWindow : WindowPtr;
- TempPort : GrafPtr;
- BEGIN
- UpdateWindow := WindowPtr(myEvent.message);
- IF UpdateWindow = PlotWindow THEN
- BEGIN
- GetPort(TempPort); {save port}
- SetPort(UpdateWindow);
- BeginUpDate(UpdateWindow);
- BackColor(whiteColor);
- EraseRect(UpdateWindow^.portRect);
- ClipRect(UpdateWindow^.portRect);
- DrawPicture(DrawingPic, PicRect);
- DrawGrowIcon(UpdateWindow);
- EndUpDate(UpdateWindow);
- SetPort(TempPort); {restore port}
- END;
- END; {of proc}
-
- PROCEDURE doActivates (myEvent : EventRecord);
- VAR
- TargetWindow : WindowPtr;
- TargetPeek : WindowPeek;
- BEGIN
- TargetWindow := WindowPtr(myEvent.message);
- TargetPeek := windowPeek(TargetWindow);
- SetPort(TargetWindow);
- IF Odd(myEvent.modifiers) THEN
- BEGIN {activate}
- IF TargetWindow = PlotWindow THEN
- BEGIN
- DisableItem(myMenus[EditM], eUndo);
- DisableItem(myMenus[EditM], eCut);
- DisableItem(myMenus[EditM], eCopy);
- DisableItem(myMenus[EditM], ePaste);
- DisableItem(myMenus[EditM], eClear);
- DrawGrowIcon(TargetWindow);
- END
- END { of activate loop}
- ELSE
- BEGIN {deactivate}
- IF TargetWindow = PlotWindow THEN
- BEGIN
- EnableItem(myMenus[EditM], eUndo);
- EnableItem(myMenus[EditM], eCut);
- EnableItem(myMenus[EditM], eCopy);
- EnableItem(myMenus[EditM], ePaste);
- EnableItem(myMenus[EditM], eClear);
- DrawGrowIcon(TargetWindow);
- END; { of my window activation}
- END; {of deactivate loop}
- END; {of proc}
-
- PROCEDURE doMulti (myEvent : EventRecord);
-
- CONST
- MouseMovedEvt = $FA;
- VAR
- HiByte : byte;
- bit0 : LongInt;
- sysresult : boolean;
- ResumeWindow : WindowPtr;
- ResumePeek : WindowPeek;
- SuspendWindow : WindowPtr;
- SuspendPeek : WindowPeek;
- MouseMove : LongAndByte;
- BEGIN
- bit0 := 31; {convert 68000 to toolbox}
- {check for mouse moved event}
- MouseMove.longView := myEvent.message;
- HiByte := Byte(MouseMove.byteView.byte0);
- IF HiByte = MouseMovedEvt THEN
- BEGIN {Handle mouse moved event}
- END;
- {check for resume event }
- IF Odd(myEvent.message) THEN
- BEGIN {resume}
- {activate Event}
- ResumeWindow := FrontWindow;
- IF ResumeWindow = PlotWindow THEN
- BEGIN
- SetPort(ResumeWindow);
- InvalRect(ResumeWindow^.portRect); { force update event}
- DisableItem(myMenus[EditM], eUndo);
- DisableItem(myMenus[EditM], eCut);
- DisableItem(myMenus[EditM], eCopy);
- DisableItem(myMenus[EditM], ePaste);
- DisableItem(myMenus[EditM], eClear);
- DrawGrowIcon(ResumeWindow);
- END;
- IF FrontWindow <> NIL THEN
- BEGIN {DA check}
- ResumePeek := WindowPeek(FrontWindow);
- IF ResumePeek^.windowKind < 0 THEN {DA}
- BEGIN
- myEvent.what := activateEvt;
- BitSet(@myEvent.modifiers, bit0);
- sysresult := SystemEvent(myEvent);
- END; {da}
- END; {DA check}
- { end of activate Event}
- END {of resume}
- ELSE
- BEGIN {suspend}
- {de-activate Event}
- SuspendWindow := FrontWindow;
- IF SuspendWindow = PlotWindow THEN
- BEGIN {plotwindow}
- SetPort(SuspendWindow);
- InvalRect(SuspendWindow^.portRect); {force update}
- EnableItem(myMenus[EditM], eUndo);
- EnableItem(myMenus[EditM], eCut);
- EnableItem(myMenus[EditM], eCopy);
- EnableItem(myMenus[EditM], ePaste);
- EnableItem(myMenus[EditM], eClear);
- DrawGrowIcon(SuspendWindow);
- END; {plotwindow}
- IF FrontWindow <> NIL THEN
- BEGIN {DA check}
- SuspendPeek := WindowPeek(FrontWindow);
- IF SuspendPeek^.windowKind < 0 THEN
- BEGIN {da}
- myEvent.what := activateEvt;
- BitClr(@myEvent.modifiers, bit0);
- sysresult := SystemEvent(myEvent);
- END; {da}
- END; {DA check}
- { end of de-activate Event}
- END; {suspend}
- END; {of proc}
-
- PROCEDURE MainEventLoop;
- CONST
- MultiFinderEvt = 15;
- VAR
- sleep : LongInt;
- Event : EventRecord;
- DoIt : Boolean;
- BEGIN
- sleep := 10;
- REPEAT
- IF WNE THEN
- DoIt := WaitNextEvent(EveryEvent, Event, sleep, NIL) {no mouse tracking}
- ELSE
- BEGIN
- SystemTask;
- DoIt := GetNextEvent(EveryEvent, Event);
- END;
- IF DoIt THEN
- CASE Event.what OF
- mouseDown :
- doMouse(Event);
- KeyDown, Autokey :
- doKeyDowns(Event);
- updateEvt :
- doUpdates(Event);
- activateEvt :
- doActivates(Event);
- MultiFinderEvt :
- doMulti(Event);
- OTHERWISE
- BEGIN
- END;
- END; {of event case}
- UNTIL Finished; {end program}
- END;
-
- { Main Program}
- BEGIN
- InitMac;
- InitRects;
- InitMyWindow;
- InitMyMenus;
- MainEventLoop;
- END.